package com.cyandpos.pad.offline.model;

import android.database.Cursor;
import android.util.Log;

import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.offline.offlinedb.CursorUtil;
import com.cyandpos.pad.offline.offlinedb.Sqldb;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.posdata.Member;
import com.cyandpos.pad.posdata.Order;
import com.cyandpos.pad.posdata.Payment;
import com.cyandpos.pad.posdata.Person;
import com.cyandpos.pad.posdata.RefundOrder;
import com.cyandpos.pad.posdata.SubTicketItem;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OfflineQueryOrder {

    public static void QueryOrderInfoEexcute(Sqldb sqldb, Map<String, Object> params, OkHttpUtils.ResultCallback callback){

        RefundOrder refundOrder=new RefundOrder();
        refundOrder.setOrderCount(1);
        refundOrder.setPageNo(1);
        refundOrder.setPageSize(1);
        List<Order> orderList=new ArrayList<>();
        if(!"".equals(params.get("orderNo")+"")&&!"null".equals(params.get("orderNo")+"")){
            Order order=getOrder(sqldb,params,params.get("orderNo")+"",callback);
            if(order==null){
                callback.onFailure("没有查到此订单"+params.get("orderNo"));
            }else {
                orderList.add(order);
                refundOrder.setOrderList(orderList);
                callback.onSuccess(CursorUtil.getOfflineBean(refundOrder));
            }
        }else if(!"".equals(params.get("jlbh")+"")&&!"null".equals(params.get("jlbh")+"")){
            Cursor cursor = sqldb.find("select * from POS_ORDER where JLBH="+"'"+params.get("jlbh")+"'");
            if(cursor!=null&&cursor.getCount()>0){
                while (cursor.moveToNext()){
                    String orderno= CursorUtil.getString(cursor,"ORDERNO");
                    Order order=getOrder(sqldb,params,orderno,callback);
                    orderList.add(order);
                }
                refundOrder.setOrderList(orderList);
                callback.onSuccess(CursorUtil.getOfflineBean(refundOrder));
            }else{
                callback.onFailure("没有查到此订单");
            }
        }
        else{
            Cursor cursor = sqldb.find("select * from POS_ORDER where JSSJ between '"+params.get("beginTime")+"'"+" and '"+params.get("endTime")+"'");
            if(cursor!=null&&cursor.getCount()>0){
                while (cursor.moveToNext()){
                    String orderno=CursorUtil.getString(cursor,"ORDERNO");
                    Order order=getOrder(sqldb,params,orderno,callback);
                    orderList.add(order);
                }
                refundOrder.setOrderList(orderList);
                callback.onSuccess(CursorUtil.getOfflineBean(refundOrder));
            }else{
                callback.onFailure("暂无订单");
            }

        }
    }


    public static Order getOrder(Sqldb sqldb, Map<String, Object> params, String orderNo, OkHttpUtils.ResultCallback callback){
        Order order=new Order();
        BigDecimal price=new BigDecimal(0);
        BigDecimal count=new BigDecimal(0);
        BigDecimal saleMoney=new BigDecimal(0);
        BigDecimal discount=new BigDecimal(0);
        String personCode="";
        params.put("orderNo",orderNo);
        List<Goods> goodsList=OfflineSale.getGoodsList(sqldb,params);
        if(goodsList!=null&&goodsList.size()>0) {
            for (Goods g : goodsList) {
                count = count.add(new BigDecimal(g.getSaleCount()));
                saleMoney = saleMoney.add(new BigDecimal(g.getSaleMoney()));
                discount = discount.add(new BigDecimal(g.getDiscount()));
            }
        }else{
            return null;
        }

        Cursor cursorMember = sqldb.find("select * from ORDER_MEM where ORDERNO="+"'"+params.get("orderNo")+"'");
        if(cursorMember!=null&&cursorMember.getCount()>0){
            while (cursorMember.moveToNext()){
                Member member=new Member();
                member.setMemberCardNo(CursorUtil.getString(cursorMember,"HYKNO"));
                member.setMemberCardTypeId(CursorUtil.getInt(cursorMember,"HYKTYPE"));
                order.setMemberInfo(member);
            }
        }

        List<Payment> payList=new ArrayList<>();
        try {
            Cursor cursor = sqldb.find("select A.ORDERNO,A.SKFS,A.SKJE,A.YYJE,A.PAYGUID,B.NAME from ORDER_PAY A,SKFS B where A.SKFS=B.CODE and A.ORDERNO="+"'"+params.get("orderNo")+"'");
            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    Payment payment = new Payment();
                    payment.setPayCode(Integer.parseInt(CursorUtil.getString(cursor, "SKFS")));
                    payment.setPayName(CursorUtil.getString(cursor, "NAME"));
                    payment.setPayAmount(CursorUtil.getString(cursor, "SKJE"));
                    payList.add(payment);
                    price = price.add(new BigDecimal(payment.getPayAmount()));
                }
                cursor.close();
            }else{
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        order.setCent("0");
        order.setSktno(params.get("sktno")+"");
        order.setOrderNo(params.get("orderNo")+"");
        order.setTotalCount(count.intValue()+"");
        order.setTotalAmount(price+"");
        order.setTotalDiscount(discount+"");
        order.setDecPreferentialMoney("0.0");
        order.setChangeAmount((price.add(discount).subtract(saleMoney))+"");
        order.setNeedBuyCent(false);
        order.setNeedVipToOfferCoupon(false);
        Cursor cursorTime = sqldb.find("select * from POS_ORDER where ORDERNO="+"'"+params.get("orderNo")+"'");
        if(cursorTime!=null&&cursorTime.getCount()>0) {
            while (cursorTime.moveToNext()){
                order.setOrderTime(CursorUtil.getString(cursorTime, "JSSJ"));
                order.setStatus(CursorUtil.getString(cursorTime, "STATUS"));
                order.setJlbh(CursorUtil.getString(cursorTime, "JLBH"));
            }
            cursorTime.close();
        }else{
            return null;
        }
        order.setPayList(payList);

        List<SubTicketItem> subTickets=new ArrayList<>();
        SubTicketItem subTicketItem=new SubTicketItem();
        subTicketItem.setGoodsList(goodsList);
        subTicketItem.setTcktInx("0");
        if(goodsList!=null&&goodsList.size()>0) {
            subTicketItem.setDeptCode(goodsList.get(0).getDeptCode());
            personCode=goodsList.get(0).getPersonId();
        }

        try {
            Cursor cursorP = sqldb.find("select R.PERSON_ID,R.PERSON_NAME,R.DEPTID,R.WORK_TYPE,R.RYDM,B.BMDM,B.DEPT_NAME,R.SHOPID from RYXX R,BM B  where R.DEPTID=B.DEPTID  and (B.FDBH=" + "'"+params.get("storeCode") +"'"+ " or B.FDBH=-1) and R.STATUS=1 AND R.PERSON_ID=" + personCode + "  and (B.FDBH=" + "'"+params.get("storeCode") + "'"+" or B.FDBH=-1) and R.STATUS=1");
            if (cursorP != null && cursorP.getCount() > 0) {
                while (cursorP.moveToNext()) {
                    Person person = new Person();
                    person.setPersonId(CursorUtil.getInt(cursorP, "PERSON_ID"));
                    person.setPersonName(CursorUtil.getString(cursorP, "PERSON_NAME"));
                    person.setPersonCode(CursorUtil.getString(cursorP, "RYDM"));
                    person.setPersonDeptId(CursorUtil.getInt(cursorP, "DEPTID"));
                    person.setPersonDeptCode(CursorUtil.getString(cursorP, "BMDM"));
                    person.setPersonDeptName(CursorUtil.getString(cursorP, "DEPT_NAME"));
                    person.setShopId(CursorUtil.getInt(cursorP, "SHOPID"));
                    subTicketItem.setPersonInfo(person);
                }
                cursorP.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        subTickets.add(subTicketItem);
        order.setSubTickets(subTickets);
        try {
            Cursor cursorOrder = sqldb.find("select ORDERNO,SKTNO,JLBH,FDBH,JYLX,DDLY,KSSJ,SKY,XSJE,STATUS,JYJF,OLDORDERNO from POS_ORDER where ORDERNO="+"'"+params.get("orderNo")+"'" );
            Log.e("TAG", "cursorOrder===" + cursorOrder.getCount());
            if (cursorOrder != null && cursorOrder.getCount() > 0) {
                while (cursorOrder.moveToNext()) {
                    int personId = CursorUtil.getInt(cursorOrder, "SKY");
                    Cursor cursorPerson = sqldb.find("select R.PERSON_ID,R.PERSON_NAME,R.DEPTID,R.WORK_TYPE,R.RYDM,B.BMDM,B.DEPT_NAME,R.SHOPID from RYXX R,BM B  where R.DEPTID=B.DEPTID AND  R.PERSON_ID=" + personId + " and (B.FDBH=" + "'"+params.get("storeCode") + "'"+" or B.FDBH=-1) and R.STATUS=1");
                    if (cursorPerson != null && cursorPerson.getCount() > 0) {
                        while (cursorPerson.moveToNext()) {
                            Person person = new Person();
                            person.setPersonId(CursorUtil.getInt(cursorPerson, "PERSON_ID"));
                            person.setPersonName(CursorUtil.getString(cursorPerson, "PERSON_NAME"));
                            person.setPersonCode(CursorUtil.getString(cursorPerson, "RYDM"));
                            person.setPersonDeptId(CursorUtil.getInt(cursorPerson, "DEPTID"));
                            person.setPersonDeptCode(CursorUtil.getString(cursorPerson, "BMDM"));
                            person.setPersonDeptName(CursorUtil.getString(cursorPerson, "DEPT_NAME"));
                            order.setCasherInfo(person);
                        }
                    }
                }
                cursorOrder.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return order;
    }
}
